package com.yeexun.service.impl;

import com.yeexun.domain.RecTo;
import com.yeexun.domain.Recording;
import com.yeexun.mapper.RecordingMapper;
import com.yeexun.service.RecordingService;
import com.yeexun.util.Result;
import com.yeexun.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author rookie
 */
@Service
public class RecordingServiceImpl implements RecordingService {

    @Autowired
    private RecordingMapper recordingMapper;

    /**
     * 根据月份查询
     * @param month
     * @param id
     * @return
     */
    @Override
    public List<RecTo> queryByMonth(Integer id, String month) {
        List<RecTo> recTos = recordingMapper.selectByMonth(id,month);
        return recTos;
    }

    /**
     * 根据年份查询
     * @param year
     * @param id
     * @return
     */
    @Override
    public List<RecTo> queryByYear(Integer id,String year) {
        List<RecTo> recTos = recordingMapper.selectByYear(id,year);

        BigDecimal  income = new BigDecimal(0);
        BigDecimal expenditure = new BigDecimal(0);
        for (RecTo recTo : recTos) {
            if (recTo.getType()==0){
                income =  recTo.getMoney();
            }else {
                expenditure = expenditure.add(recTo.getMoney());
            }
        }
        //年度余额
        RecTo recTo = new RecTo();
        recTo.setMoney(income.subtract(expenditure));
        recTo.setType(6);
        recTos.add(recTo);
        return recTos;
    }

    /**
     * 月账单列表
     * @param id
     * @param month
     * @return
     */
    @Override
    public List<Recording> queryMonth(Integer id, String month) {
        List<Recording> recordings = recordingMapper.selectMonth(id, month);

        return recordings;
    }

    /**
     * 年账单列表
     * @param id
     * @param year
     * @return
     */
    @Override
    public List<Recording> queryYear(Integer id, String year) {
        List<Recording> recordings = recordingMapper.selectYear(id, year);
        return recordings;
    }

    /**
     * 新增
     * @param recording
     * @return
     */
    @Override
    public Result save(Recording recording) {
        recordingMapper.insert(recording);
        return ResultGenerator.genSuccessResult(recording);
    }

    /**
     * 计算账户余额
     * @param userId
     * @return
     */
    @Override
    public BigDecimal selectSumMoney(Integer userId) {

        BigDecimal sumMoney = recordingMapper.selectSumMoney(userId, 0);

        BigDecimal bigDecimal = recordingMapper.selectSumMoney1(userId, 0);

        if (sumMoney==null){
            sumMoney=new BigDecimal(0);
        }
        if (bigDecimal==null){
            bigDecimal=new BigDecimal(0);
        }
        BigDecimal newSumMoney = sumMoney.subtract(bigDecimal);

        return newSumMoney;
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteBill(Integer id) {
        recordingMapper.deleteByPrimaryKey(id);

        return ResultGenerator.genSuccessResult();
    }
}
